//
//  PurchaseCommodityTableViewCell.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/28.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "PurchaseCommodityTableViewCell.h"

#import "KKCurrencyPickerView.h"

@implementation PurchaseCommodityTableViewCell

- (void)awakeFromNib {
    
    _kk_itemNameLabel.contentAlignment = KKLabelContentAlignmentTop;
}

#pragma mark - UITextField methods

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == _kk_itemPurchasePriceField) {
        
        double price = [textField.text doubleValue];
        [_kk_item setKk_price: @(price)];
        [_kk_item updateAmountWithCurrency: nil];
        
        [self updateItemDataWithCurrency: nil];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return true;
}

#pragma mark - Private methods

- (void)updateItemDataWithCurrency:(NSString *)updateCurrency {

    if (updateCurrency != nil) {
        [_kk_item updateAmountWithCurrency: updateCurrency];
    }
    
    [_kk_PurchasePriceTypeButton setTitle: _kk_item.kk_currency forState: UIControlStateNormal];
    
    _kk_itemPurchasePriceField.text = [NSString stringWithFormat: @"%.2f", [_kk_item.kk_price doubleValue]];
    _kk_itemPriceField.text = [NSString stringWithFormat: @"%.2f", [_kk_item.kk_amount doubleValue]];
    
    [[NSNotificationCenter defaultCenter] postNotificationName: NSNotificationNameIsPurchaseCommodityListAmountName object: nil];
}

#pragma mark - Setter methods

- (void)setKk_item:(KKPurchaseCommodityItem *)kk_item {
    
    _kk_item = kk_item;
    _kk_itemNameLabel.text = _kk_item.kk_name;
    
    NSInteger number = [_kk_item.kk_number integerValue];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [self updateItemDataWithCurrency: nil];
}


#pragma mark - Action methods

- (IBAction)actionOfCountReduce:(UIButton *)sender {
    
    NSInteger number = [_kk_item.kk_number integerValue];
    number--;
    
    [_kk_item setKk_number: @(number)];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [[NSNotificationCenter defaultCenter] postNotificationName: NSNotificationNameIsPurchaseCommodityListAmountName object: nil];    
}

- (IBAction)actionOfCountIncrease:(UIButton *)sender {
    
    NSInteger number = [_kk_item.kk_number integerValue];
    number++;
    
    [_kk_item setKk_number: @(number)];
    
    _kk_numberLabel.text = [NSString stringWithFormat: @"%@",@(number)];
    _kk_reduceButton.enabled = number;
    
    [[NSNotificationCenter defaultCenter] postNotificationName: NSNotificationNameIsPurchaseCommodityListAmountName object: nil];
}

- (IBAction)actionOfSelectCurrency:(UIButton *)sender {
    
    NSString *title = sender.titleLabel.text;
    
    KKCurrencyPickerView *pickerView = [KKCurrencyPickerView pickerView];
    [pickerView showWithTitle: title];
    
    __weak typeof(self) weakSelf = self;
    pickerView.selectedBlock = ^(NSString *selected){
        
        [weakSelf updateItemDataWithCurrency: selected];
    };
}

@end
